user_movie_ratings4 = {}
# user_movie_ratings4['华映科技']={'捷荣技术': 3, '荣联科技': 4, '宁德时代': 4, '远东传动': 4}
# user_movie_ratings4['捷荣技术']={'华映科技': 1, '远东传动': 1, '荣联科技': 1, '宁德时代': 1, '林州重工': 1}
# user_movie_ratings4['瑞贝卡']={'华映科技': 1, '深科技': 4}
# user_movie_ratings4['荣联科技']={'林州重工': 4, '瑞贝卡': 2}
# user_movie_ratings4['冠石科技']= {'荣联科技': 5, '华映科技': 3, '捷荣技术': 4}
# user_movie_ratings4['远东传动']= {'华映科技': 3}


import math
import pandas
import csv
import json

resultModels = {}

# target : code , 次数
# 统计次数
def formatData(code, relateCode, count):
    resultModels[code][relateCode] = count

# 余弦定理
def cosine_similarity(a, b):
    numerator = sum([a[key] * b[key] for key in a if key in b])
    denominator = math.sqrt(sum([a[key]**2 for key in a])) * math.sqrt(sum([b[key]**2 for key in b]))
    return numerator / denominator

# 推荐
def recommend_stock(target, models):
    # 计算目标与其他的相似度
    similarities = {other: cosine_similarity(models[target], models[other]) for other in models if other != target}
    # 按相似度降序排列stock
    sorted_items = sorted(similarities, key=similarities.get, reverse=True)
    # 找到与目标stock最相似的stock
    most_similar_item = sorted_items[0]
    # 找到最相似stock喜欢的电影，但目标stock未看过的电影
    recommended_items = {movie: rating for movie, rating in models[most_similar_item].items() if movie not in models[target]}
    # 按评分降序排列推荐电影
    sorted_recommended_item = sorted(recommended_items, key=recommended_items.get, reverse=True)
    return sorted_recommended_item

# 为Alice推荐电影
# data  = make_json(r"D:\work\__WorkPlace\pythonWork\test.csv")
# recommended_movies = recommend_movies('远东传动', user_movie_ratings4)
# print(f"Recommended movies for Alice: {recommended_movies}")

# main方法
if __name__ == "__main__":
    print()

recommended_movies = recommend_stock('华映科技', user_movie_ratings4)
print(f"Recommended movies for Alice: {recommended_movies}")

# print(user_movie_ratings3)
